package com.ymatou.shop.ui.activity.seller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ymatou.shop.Constants;
import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.ui.activity.BaseHeaderListActivity;
import com.ymatou.shop.ui.adapter.BaseAdapter;
import com.ymatou.shop.ui.view.PullToRefreshView;
import com.ymatou.shop.ui.view.item.OrderProductItemView;

public class OrderActivity extends BaseHeaderListActivity {
	
	private static final int REQUEST_ID_LIST = 0x01;
	
	private BaseAdapter mAdapter;
	private List<Product> mProductList;
	
	private String mLastId;
	private volatile boolean mReachEnd;
	private volatile boolean mLoadingList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		
		mProductList = new ArrayList<Product>();
		mAdapter = new ProductListAdapter(this, mProductList);
		
		setupViews();
		setupData(true);
	}
	
	@Override
	protected void setupViews() {
		super.setupViews();
		getTitleTextView().setText(R.string.title_order);
		getTitleLeftImageButton().setImageResource(R.drawable.back_icon);
		getTitleLeftImageButton().setVisibility(View.VISIBLE);
		
		getTitleRightImageButton1().setImageResource(R.drawable.refresh_icon);
		getTitleRightImageButton1().setVisibility(View.VISIBLE);
		getTitleRightImageButton1().setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getPullToRefreshView().updateWithoutOffset();
				getPullToRefreshView().scrollToUpdate();
				setupData(false);
			}
		});
		
		getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				Intent intent = new Intent(v.getContext(), OrderDetailActivity.class);
				intent.putExtra(Constants.EXTRA_DATA, mProductList.get(position));
				v.getContext().startActivity(intent);
			}
		});
		getPullToRefreshView().setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				mLastId = null;
				setupData(false);
			}
		});
		
		getListView().setAdapter(mAdapter);
		
		changeLoadingViewState(false);
	}
	
	@Override
	protected void setupData(boolean useCache) {
		if (!mReachEnd) {
			if (!mLoadingList) {
				RequestInfo requestInfo = ApiRequest.getSellerProductList(useCache);
				getCacheManager().register(REQUEST_ID_LIST, requestInfo, this);
			}
		}
	}
	
	@Override
	public BaseAdapter getAdapter() {
		return mAdapter;
	}
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo,
			Object response) {
		if (requestId == REQUEST_ID_LIST) {
			//////////test///
			if (mProductList.size() == 0) {
				Product p = new Product();
				p.mSaleNum = 100;
				p.mStockNum = 50;
				p.mDownPayment = 80;
				p.mPrice = 100;
				p.mDescription = "开始！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！" +
						"测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！测试描述，开始卖了！结束！";
				mProductList.add(p);mProductList.add(p);mProductList.add(p);mProductList.add(p);mProductList.add(p);
				mProductList.add(p);mProductList.add(p);mProductList.add(p);mProductList.add(p);mProductList.add(p);
				
				changeLoadingViewState(true);
			}
			
			if (mProductList.size() > 0) {
				getPullToRefreshView().onRefreshComplete(new Date());
			} else {
				getPullToRefreshView().setLastUpdated(new Date());
			}
			return;
			/////////////
//			List<Product> list = ApiResponse.getProductList((String) response);
//			if (list == null) {
//				if (mProductList.size() == 0) {
//					changeErrorViewState(false);
//				}
//				return;
//			}
//			
//			if (mProductList.size() == 0) {
//				mProductList.addAll(list);
//				
//				getPullToRefreshView().setLastUpdated(new Date());
//				
//				changeLoadingViewState(true);
//			} else {
//				if (getPullToRefreshView().isRefresh()) {
//					mProductList.clear();
//				}
//				mProductList.addAll(list);
//				mAdapter.notifyDataSetChanged();
//				getPullToRefreshView().onRefreshComplete(new Date());
//			}
//			
//			if (list.isEmpty()) {
//				mReachEnd = true;
//			} else {
//				mLastId = list.get(list.size() - 1).mId;
//			}
//			mLoadingList = false;
		} else {
			super.onCacheSuccess(requestId, requestInfo, response);
		}
	}
	
	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo,
			CacheException error) {
		if (requestId == REQUEST_ID_LIST) {
			changeErrorViewState(false);
		} else {
			super.onCacheFailed(requestId, requestInfo, error);
		}
	}
	
	private static class ProductListAdapter extends BaseAdapter<Product> {
		
		public ProductListAdapter(Context context, List<Product> items) {
			super(context, items);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			OrderProductItemView itemView;
			if (convertView == null) {
				itemView = new OrderProductItemView(getContext(), mOnIconLoadCompleteListener);
			} else {
				itemView = (OrderProductItemView) convertView;
			}
			
			Product item = getItem(position);
			itemView.setData(item);
			
			return itemView;
		}
		
		private ImageCache.OnIconLoadCompleteListener mOnIconLoadCompleteListener = new ImageCache.OnIconLoadCompleteListener() {
			@Override
			public void onLoadComplete(String url, boolean success, Drawable drawable) {
				if (success) {
					notifyDataSetChanged();
				}
			}
		};
	}
}
